﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
using School.Common;
using School.Admission;

namespace School.Pages.Organization
{
    public partial class CongratulatoryMessagesSearch : System.Web.UI.Page
    {
        //***************** This is function to handle page when page load ***************************
        //-**********************************************************************
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                ((HtmlForm)this.Master.FindControl("form1")).Target = "_blank";

                if (x.Contains(1014) == true)
                {

                    if (!(Page.IsPostBack))
                    {


                        Fillcrls();

                        InitializeControls();

                        GridDataBind();

                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        # region "Collect Data"

        //***************** This is function to bind data grid  ***************************
        //-**************************************************************************
        protected void GridDataBind()
        {
            Int32 mycount;
            Hashtable SearchTable = new Hashtable();

            if (txtSearchFileNumber.Text.Length > 0)
            {
                SearchTable.Add("FileNumber", txtSearchFileNumber.Text.ToString());
            }
            if (ddlSearchcampus_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Campus_Id", ddlSearchcampus_Id.SelectedValue.ToString());
            }

            if (ddlSearchCertificate_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Certificate_Id", ddlSearchCertificate_Id.SelectedValue.ToString());
            }
            if (ddlSearchStage_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Stage_Id", ddlSearchStage_Id.SelectedValue.ToString());
            }
            if (ddlSearchGrade_ID.SelectedIndex > 0)
            {
                SearchTable.Add("Grade_ID", ddlSearchGrade_ID.SelectedValue.ToString());
            }
            if (ddlSearchNationality_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Nationality_Id", ddlSearchNationality_Id.SelectedValue.ToString());
            }
            if (ddlFQualification_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Qualification_Id", ddlFQualification_Id.SelectedValue.ToString());
            }
            if (ddlFCareer.SelectedIndex > 0)
            {
                SearchTable.Add("Career_Id", ddlFCareer.SelectedValue.ToString());
            }
            if (txtFSpecialization.Text.Length > 0)
            {
                SearchTable.Add("Specialization", txtFSpecialization.Text.ToString());
            }
            if (txtFPointOfGraduation.Text.Length > 0)
            {
                SearchTable.Add("PointOfGraduation", txtFPointOfGraduation.Text.ToString());
            }
            if (txtStreetName.Text.Length > 0)
            {
                SearchTable.Add("StreetName", txtStreetName.Text.ToString());
            }
            if (ddlDistrict_Id.SelectedIndex > 0)
            {
                SearchTable.Add("District_Id", ddlDistrict_Id.SelectedValue.ToString());
            }
            if (txtSearchRegistrationNumber.Text.Length > 0)
            {
                SearchTable.Add("RegistrationNumber", txtSearchRegistrationNumber.Text.ToString());
            }
            if (txtSearchName.Text.Length > 0)
            {
                switch (rblSearchType.SelectedIndex)
                {
                    case 0:
                        SearchTable.Add("SD_FullName", txtSearchName.Text.ToString() + "%");
                        break;
                    case 1:
                        SearchTable.Add("SD_FullName", "%" + txtSearchName.Text.ToString() + "%");
                        break;
                    case 2:
                        SearchTable.Add("SD_FullName", txtSearchName.Text.ToString());
                        break;
                }
            }
            String RelTab = "";
            String RelTabDef = "";
            if (rblRelatives.SelectedIndex >= 0)
            {
                switch (rblRelatives.SelectedIndex)
                {
                    case 0:
                        RelTab = "SD_Father";
                        RelTabDef = "Father";
                        break;
                    case 1:
                        RelTab = "SD_Mother";
                        RelTabDef = "";
                        break;
                    case 2:
                        RelTab = "SD_Custody";
                        RelTabDef = "Custody";
                        break;
                }
            }

            if (txtRelativeName.Text.Length > 0)
            {
                switch (rblSearchType0.SelectedIndex)
                {
                    case 0:
                        SearchTable.Add("Relative_FullName", txtRelativeName.Text.ToString() + "%");
                        break;
                    case 1:
                        SearchTable.Add("Relative_FullName", "%" + txtRelativeName.Text.ToString() + "%");
                        break;
                    case 2:
                        SearchTable.Add("Relative_FullName", txtRelativeName.Text.ToString());
                        break;
                }
            }
            if (ddlParentsSocialStatus_Id.SelectedIndex > 0)
            {
                SearchTable.Add("ParentsSocialStatus_Id", ddlParentsSocialStatus_Id.SelectedValue.ToString());
            }
            if (ddlCustodyType_Id.SelectedIndex > 0)
            {
                SearchTable.Add("CustodyType_Id", ddlCustodyType_Id.SelectedValue.ToString());
            }
            if (ddlStudentLivingPlace_Id.SelectedIndex > 0)
            {
                SearchTable.Add("StudentLivingPlace_Id", ddlStudentLivingPlace_Id.SelectedValue.ToString());
            }
            //IEnumerable<TE_Student> AllStudentsData = Student.Mysearch(SearchTable, RelTab, RelTabDef);

            long campus = 0;
            long certificate = 0;
            long stage = 0;
            long grade = 0;
            string name = txtSearchName.Text;

            long.TryParse(ddlSearchcampus_Id.SelectedValue, out campus);
            long.TryParse(ddlSearchCertificate_Id.SelectedValue, out certificate);
            long.TryParse(ddlSearchStage_Id.SelectedValue, out stage);
            long.TryParse(ddlSearchGrade_ID.SelectedValue, out grade);

            var AllStudentsData = Students.SearchFiltered(campus, certificate, stage, grade, name);
            griddata.DataSource = AllStudentsData;//AllStudentsData;

            griddata.DataBind();
            griddata.Visible = true;
            mycount = AllStudentsData.Count();
            lblRecordNo.Text = "عدد نتائج البحث = " + mycount.ToString();
            if (mycount == 0)
            {
                lblMessage.Text = "لا يوجد بيانات";
            }
            else
            {
                lblMessage.Text = "";
            }

        }

        # endregion


        # region "Fill all crls"

        //***************** This is function to fill crl  ***************************
        //-**************************************************************************

        protected void Fillcrls()
        {
            ddlSearchcampus_Id.DataSource = Structure.GetCampuses();
            ddlSearchcampus_Id.DataTextField = "Name";
            ddlSearchcampus_Id.DataValueField = "Id";
            ddlSearchcampus_Id.DataBind();
            ddlSearchcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));



            ddlFQualification_Id.DataSource = LookUps.Qualifications();
            ddlFQualification_Id.DataTextField = "Name";
            ddlFQualification_Id.DataValueField = "Id";
            ddlFQualification_Id.DataBind();
            ddlFQualification_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlFCareer.DataSource = LookUps.Jobs();
            ddlFCareer.DataTextField = "Name";
            ddlFCareer.DataValueField = "Id";
            ddlFCareer.DataBind();
            ddlFCareer.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlDistrict_Id.DataSource = LookUps.Districts();
            ddlDistrict_Id.DataTextField = "Name";
            ddlDistrict_Id.DataValueField = "Id";
            ddlDistrict_Id.DataBind();
            ddlDistrict_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlSearchNationality_Id.DataSource = LookUps.Countries();
            ddlSearchNationality_Id.DataTextField = "Name";
            ddlSearchNationality_Id.DataValueField = "Id";
            ddlSearchNationality_Id.DataBind();
            ddlSearchNationality_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlParentsSocialStatus_Id.DataSource = LookUps.ParentsSocialStatus();
            ddlParentsSocialStatus_Id.DataTextField = "Name";
            ddlParentsSocialStatus_Id.DataValueField = "Id";
            ddlParentsSocialStatus_Id.DataBind();
            ddlParentsSocialStatus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlCustodyType_Id.DataSource = LookUps.CustodyType();
            ddlCustodyType_Id.DataTextField = "Name";
            ddlCustodyType_Id.DataValueField = "Id";
            ddlCustodyType_Id.DataBind();
            ddlCustodyType_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlStudentLivingPlace_Id.DataSource = LookUps.LivingWith();
            ddlStudentLivingPlace_Id.DataTextField = "Name";
            ddlStudentLivingPlace_Id.DataValueField = "Id";
            ddlStudentLivingPlace_Id.DataBind();
            ddlStudentLivingPlace_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

        }


        //***************** This is function to initlaize crl  ***************************
        //-**************************************************************************
        protected void InitializeControls()
        {
            txtSearchFileNumber.Text = "";
            txtSearchRegistrationNumber.Text = "";
        }
        protected void InitializeRelControls()
        {
            txtRelativeName.Text = "";
            ddlSearchNationality_Id.SelectedIndex = 0;
            ddlStudentLivingPlace_Id.SelectedIndex = 0;
            ddlParentsSocialStatus_Id.SelectedIndex = 0;
            ddlFQualification_Id.SelectedIndex = 0;
            ddlSearchNationality_Id.SelectedIndex = 0;
            ddlCustodyType_Id.SelectedIndex = 0;
            txtFSpecialization.Text = "";
            txtCRelationToStudent.Text = "";
            txtFCompanyName.Text = "";
            txtFPointOfGraduation.Text = "";
            txtStreetName.Text = "";
        }
        # endregion


        # region "Mode Handlers"

        //***************** This is function to handle page in add mode  ***************************
        //-**************************************************************************
        protected void PutFormInAddMode()
        {
            //txtFirstName.Text = "";
            //txtSecondName.Text = "";
            //txtThirdName.Text = "";
            //txtFourthName.Text = "";
        }


        //***************** This is function to handle page in brows mode  ***************************
        //-**************************************************************************

        protected void PutFormInBrwosMode()
        {
            pnlbrows.Visible = false;

        }

        #endregion


        # region "Grid Events ***********"

        //***************** This is function to handle page when Page Index Changing   ***************************
        //-**************************************************************************
        protected void griddata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griddata.PageIndex = e.NewPageIndex;
            GridDataBind();
        }


        //***************** This is function to handle page when Row Deleting  ***************************
        //-**************************************************************************

        protected void griddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                griddata.SelectedIndex = e.RowIndex;

                long CID = Int32.Parse(griddata.SelectedDataKey.Value.ToString());
                Boolean successadd = FileOrder.Delete(CID);
                if (successadd == true)
                {
                    GridDataBind();
                    lblMessage.Visible = true;
                    lblMessage.Text = "Record(s) was deleted successfully";

                }
            }
            catch (Exception)
            {
                lblMessage.Text = "Record(s) cannot be deleted, please contact your administrator";

            }

        }


        //***************** This is function to handle page when Row Data Bound  ***************************
        //-**************************************************************************
        protected void griddata_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton LB = (LinkButton)e.Row.FindControl("LinkButton1");
                LB.Attributes.Add("onclick", "javascript:return " +
                "confirm('Are you sure you want to delete record(s) ( " +
                DataBinder.Eval(e.Row.DataItem, "Id") + " ) ?')");

            }
        }

        protected void griddata_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlbrows.Visible = false;
            pnldetails.Visible = true;
            DetailsView1bind();
        }

        protected void DetailsView1bind()
        {
            Hashtable SearchTable = new Hashtable();

            SearchTable.Add("Id", griddata.SelectedDataKey.Value.ToString());
            DetViewdata.DataSource = Student.All(SearchTable);
            DetViewdata.DataBind();
        }



        # endregion









        # region " Search *********************"




        //***************** This is function to search by  Get All btn ***************************
        //-**************************************************************************
        protected void btnGetAll_Click(object sender, EventArgs e)
        {

            InitializeControls();
            GridDataBind();
        }



        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridDataBind();
        }

        # endregion

        protected void txtShowCampus_TextChanged(object sender, EventArgs e)
        {

        }

        protected void ddlSearchcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnback_Click(object sender, EventArgs e)
        {

        }

        protected void rblRelatives_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblRelatives.SelectedIndex == 2)
            {
                pnl4.Visible = true;
                ddlFCareer.SelectedIndex = 0;
                ddlFCareer.Visible = false;
            }
            else
            {
                pnl4.Visible = false;
            }

        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox1.Checked == true)
            {
                pnl2.Visible = true;
                rblRelatives.SelectedIndex = 0;
                pnl4.Visible = false;
            }
            else
            {
                pnl2.Visible = false;
                rblRelatives.SelectedIndex = -1;

            }
            InitializeRelControls();
        }

        protected void ddlSearchcampus_Id_SelectedIndexChanged1(object sender, EventArgs e)
        {

            {
                ddlSearchCertificate_Id.Items.Clear();
                ddlSearchGrade_ID.Items.Clear();
                ddlSearchStage_Id.Items.Clear();
                ddlSearchCertificate_Id.DataSource = Structure.GetCertificatesByCampus(Convert.ToInt64(ddlSearchcampus_Id.SelectedValue));
                ddlSearchCertificate_Id.DataTextField = "Name";
                ddlSearchCertificate_Id.DataValueField = "Id";
                ddlSearchCertificate_Id.DataBind();
                ddlSearchCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlSearchCertificate_Id.Enabled = true;
                ddlSearchGrade_ID.Enabled = false;
                ddlSearchStage_Id.Enabled = false;
            }
        }

        protected void ddlSearchCertificate_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSearchCertificate_Id.SelectedIndex > 0)
            {
                ddlSearchStage_Id.Items.Clear();
                ddlSearchGrade_ID.Items.Clear();
                ddlSearchStage_Id.DataSource = Structure.GetStagesByCertificate(Convert.ToInt64(ddlSearchCertificate_Id.SelectedValue));
                ddlSearchStage_Id.DataTextField = "Name";
                ddlSearchStage_Id.DataValueField = "id";
                ddlSearchStage_Id.DataBind();
                ddlSearchStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlSearchStage_Id.Enabled = true;
                ddlSearchGrade_ID.Enabled = false;
            }
        }

        protected void ddlSearchStage_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSearchStage_Id.SelectedIndex > 0)
            {

                ddlSearchGrade_ID.Items.Clear();
                ddlSearchGrade_ID.DataSource = Structure.GetGradesByStage(Convert.ToInt64(ddlSearchStage_Id.SelectedValue));
                ddlSearchGrade_ID.DataTextField = "Name";
                ddlSearchGrade_ID.DataValueField = "id";
                ddlSearchGrade_ID.DataBind();
                ddlSearchGrade_ID.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlSearchGrade_ID.Enabled = true;
            }
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            Session["stcampus"] = Session["stcertificate"] = Session["ststage"] = Session["stgrade"] = string.Empty;
            Session["stname"] = txtSearchName.Text;
            Session["stFileNumber"] = txtSearchFileNumber.Text;
            Session["stReistrationNumber"] = txtSearchRegistrationNumber.Text;
            if (ddlSearchcampus_Id.SelectedIndex > 0)
                Session["stcampus"] = ddlSearchcampus_Id.SelectedValue;
            if (ddlSearchCertificate_Id.SelectedIndex > 0)
                Session["stcertificate"] = ddlSearchCertificate_Id.SelectedValue;
            if (ddlSearchStage_Id.SelectedIndex > 0)
                Session["ststage"] = ddlSearchStage_Id.SelectedValue;
            if (ddlSearchGrade_ID.SelectedIndex > 0)
                Session["stgrade"] = ddlSearchGrade_ID.SelectedValue;

            Session["NotificationName"] = txtNotificationName.Text;
            Session["NotitficationContent"] = txtNotificationContent.Text;

            Response.Redirect("/Organization/Reports/showMessagesReport.aspx");
        }                                                                                         


    }
}